package com.example.spider;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.ReUtil;
import org.shoulder.core.util.JsonUtils;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author lym
 */
public class Why10OfflineImageUrlParser {

    static Map<String, Map<String, String>> resultMap = new HashMap<>(500);

    public static void main(String[] args) {
        List<File> files = FileUtil.loopFiles("F:\\spider\\why10WithHtml");
        for (File file : files) {
            FileReader fileReader = new FileReader(file);
            String txt = fileReader.readString();
            String id = ReUtil.findAllGroup1("post\\-(\\d+)\\.txt", file.getName()).get(0);
            String url = "https://10why.net/post/" + id + ".html";
            String html = txt.substring(txt.indexOf("<!DOCTYPE html>"));

            // 去掉
            if (html.contains("attachment-gridlove-single size-gridlove-single wp-post-image")) {
                // 第一种 https://10why.net/post/4583.html
                html = html.substring(html.lastIndexOf("<i class=\"fa fa-bars\"></i>"));
            } else if (html.contains("attachment-gridlove-cover size-gridlove-cover wp-post-imag")) {
                // 第二种 https://10why.net/post/4569.html
                html = html.substring(html.indexOf("<span class=\"gridlove-cover\">"));
            } else {
                // 视频跳过 https://10why.net/post/10620.html
                continue;
            }

            String imageName = "";
            // 图片名，可能没有
            List<String> imageNameMatchResult = ReUtil.findAllGroup1("class=\"wp-caption-text\">(.*?)</figure>", html);
            if (!imageNameMatchResult.isEmpty()) {
                imageName = imageNameMatchResult.get(0);
            } else {
                // 使用标签拼接作为图片名
                String tagRegex = "<a href=\".*?&#039; Tag\">(.*?)</a>";
                List<String> tagList = ReUtil.findAllGroup1(tagRegex, html);
                if (!CollectionUtils.isEmpty(tagList)) {
                    StringJoiner sj = new StringJoiner("-");
                    tagList.forEach(sj::add);
                    imageName = sj.toString();
                } else {
                    // 不要了
                    continue;
                }
            }
            // imageUrl
            String imageUrlRex = "src=\"(.*?)\" class=\"attachment-gridlove-single size-gridlove-single wp-post-image";
            List<String> imageUrlList = ReUtil.findAllGroup1(imageUrlRex, html);
            String imageUrl;
            if (imageUrlList.isEmpty()) {
                imageUrlRex = "src=\"(.*?)\" class=\"attachment-gridlove-cover size-gridlove-cover wp-post-imag";
                imageUrlList = ReUtil.findAllGroup1(imageUrlRex, html);
                if (imageUrlList.isEmpty()) {
                    // 不要了,视频
                    System.out.println(file.getName());
                    continue;
                }
                imageUrl = imageUrlList.get(0);
            } else {
                imageUrl = imageUrlList.get(0);
            }
            resultMap.put(url, Collections.singletonMap(imageName, imageUrl));
        }
        // 保存到文件
        String fileName = "F:/spider/why10/imageUrl.txt";
        File file = FileUtil.touch(fileName);
        FileUtil.writeString(JsonUtils.toJson(resultMap), file, StandardCharsets.UTF_8);
    }


}
